<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class CourtsController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'Courts';

/**
 * Default helper
 *
 * @var array
 */
	public $helpers = array();

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array('Court', 'FieldType');
	
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index');
	}
	
	public $timeOptions = array(
	    '00:00:00' => '00:00',
	    '01:00:00' => '01:00',
	    '02:00:00' => '02:00',
	    '03:00:00' => '03:00',
	    '04:00:00' => '04:00',
	    '05:00:00' => '05:00',
	    '06:00:00' => '06:00',
	    '07:00:00' => '07:00',
	    '08:00:00' => '08:00',
	    '09:00:00' => '09:00',
	    '10:00:00' => '10:00',
	    '11:00:00' => '11:00',
	    '12:00:00' => '12:00',
	    '13:00:00' => '13:00',
	    '14:00:00' => '14:00',
	    '15:00:00' => '15:00',
	    '16:00:00' => '16:00',
	    '17:00:00' => '17:00',
	    '18:00:00' => '18:00',
	    '19:00:00' => '19:00',
	    '20:00:00' => '20:00',
	    '21:00:00' => '21:00',
	    '22:00:00' => '22:00',
	    '23:00:00' => '23:00',
	);
	
	public function isAuthorized($user) {
		if (in_array($user['role'], array('admin', 'superAdmin'))) {
			return true;
		}
		if(in_array($this->action, array('index'))) {
			return true;
		}
		return false;
	}
	
	public function index() {
		$this->set('title_for_layout', 'Korty - strona główna');
		$data = $this->paginate('Court');
		$this->set('courts', $data);
	}
	
	public function fieldType() {
		$data = $this->paginate('FieldType');
		$this->set('fieldTypes', $data);
		if($this->request->is('post')) {
			$this->FieldType->set($this->data);
			if($this->FieldType->save($this->request->data)){
				$this->Session->setFlash('Typ kortu został dodany.', 'flash_success');
			} else {
				$this->Session->setFlash('Nie udało się dodać typu kortu.', 'flash_failure');
			}
			$this->redirect(array('action'=>'fieldType'));
		}	
	}

	public function addType() {
		$this->set('title_for_layout', 'Korty - dodaj typ kortu');
	}
	
	public function editType($id = null) {
		$this->set('title_for_layout', 'Korty - edytuj typ kortu');
		$this->FieldType->id = $id;
		if ($this->request->is('post') || $this->request->is('put')) {
			//$this->FieldType->setValidate();
			if ($this->FieldType->save($this->request->data)) {
				$this->Session->setFlash('Zapisano zmiany.', 'flash_success');
				$this->redirect(array('action' => 'fieldType'));
			} else {
				$this->Session->setFlash('Nie udało się zapisać. Proszę spróbować ponownie.', 'flash_failure');
			}
		} else {
			$this->request->data = $this->FieldType->read();
		}
	}
	
	public function add() {
		$this->set('title_for_layout', 'Korty - dodaj kort');
		$this->set('field_types', $this->FieldType->find('list', array('fields' => array('FieldType.id', 'FieldType.name'))));
		$this->set('time_options', $this->timeOptions);
		if($this->request->is('post')) {
			$this->Court->set($this->data);
			if($this->Court->save($this->request->data)){
				$this->Session->setFlash('Kort został dodany.', 'flash_success');
				$this->redirect(array('action'=>'add'));
			} else {
				$this->Session->setFlash('Nie udało się dodać kortu.', 'flash_failure');
			}
		}
	}
	
	public function edit($id = null) {
		$this->set('title_for_layout', 'Korty - edytuj kort');
		$this->set('field_types', $this->FieldType->find('list', array('fields' => array('FieldType.id', 'FieldType.name'))));
		$this->set('time_options', $this->timeOptions);
		$this->Court->id = $id;
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Court->save($this->request->data)) {
				$this->Session->setFlash('Zapisano zmiany.', 'flash_success');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Nie udało się zapisać. Proszę spróbować ponownie.', 'flash_failure');
			}
		} else {
			$this->request->data = $this->Court->read();
		}
	}
	
	public function delete($id = null) {					// TODO: usuwanie rezerwacji dla kortu
		$this->Court->id = $id;
		if (!$this->Court->exists()) {
			$this->Session->setFlash('Nie ma takiego kortu.', 'flash_failure');
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Court->delete()) {
			$this->Session->setFlash('Kort usunięty.', 'flash_success');
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash('Nie udało się usunąć kortu.', 'flash_failure');
		$this->redirect(array('action' => 'index'));
	}
	
}
